//===============================================================================
// Microsoft patterns & practices
// Composite Application Guidance for Windows Presentation Foundation
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using P2.Framework.BOLBase;
using P2.Framework.Helper;
namespace P2.SimpleAccounting.Modules.FiAccount
{
    using System;
    using System.Windows;
    using Microsoft.Practices.Unity;
    using P2.SimpleAccounting.Infrastructure;

    using P2.SimpleAccounting.Modules.FiAccount.Controllers;

    public class FiAccountsPresenter
    {
        private IUnityContainer container;
        private IRegionManager regionManager;
        private IFiAccountsListPresenter listPresenter;
        //private IFiAccountsController fiAccountController;

        public FiAccountsPresenter(
            IFiAccountsView view,
            IFiAccountsListPresenter listPresenter,
            //IFiAccountsController fiAccountController,
            IUnityContainer container, 
            IRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
            this.View = view;
            this.listPresenter = listPresenter;
            this.listPresenter.FiAccountSelected += new EventHandler<DataEventArgs<BusinessEntities.FiAccount>>(this.OnFiAccountSelected);
            this.listPresenter.FiAccountOpened += new EventHandler<DataEventArgs<BusinessEntities.FiAccount>>(this.OnFiAccountOpened);
            //this.fiAccountController = fiAccountController;

            View.SetHeader(listPresenter.View);
        }

        public IFiAccountsView View { get; set; }

        public void OnFiAccountSelected(object sender, DataEventArgs<BusinessEntities.FiAccount> e)
        {
            //fiAccountController.OnFiAccountSelected(e.Value);
            //IRegion detailsRegion = regionManager.Regions[RegionNames.DetailsRegion];
            //object existingView = detailsRegion.GetView(fiAccount.FiAccountID.ToString(CultureInfo.InvariantCulture));

            //if (existingView == null)
            //{
            //    //--AM--
            //    //IProjectsListPresenter projectsListPresenter = this.container.Resolve<IProjectsListPresenter>();
            //    //projectsListPresenter.SetProjects(fiAccount.FiAccountID);
            //    //--AM--

            //    IFiAccountsPreviewPresenter detailsPresenter = this.container.Resolve<IFiAccountsPreviewPresenter>();
            //    detailsPresenter.SetSelectedFiAccount(fiAccount);

            //    IRegionManager detailsRegionManager = detailsRegion.Add(detailsPresenter.View, fiAccount.FiAccountID.ToString(CultureInfo.InvariantCulture), true);
            //    IRegion region = detailsRegionManager.Regions[RegionNames.TabRegion];

            //    //--AM--
            //    //region.Add(projectsListPresenter.View, "CurrentProjectsView");
            //    //--AM--

            //    detailsRegion.Activate(detailsPresenter.View);
            //}
            //else
            //{
            //    detailsRegion.Activate(existingView);
            //}
        }

        public void OnFiAccountOpened(object sender, DataEventArgs<BusinessEntities.FiAccount> e)
        {
            //fiAccountController.OnFiAccountOpened(e.Value);
            IFiAccountsDetailsView dv = this.container.Resolve<IFiAccountsDetailsView>();
            //FiAccountsDetailsView dv = new FiAccountsDetailsView();
            Services.IFiAccountService service = this.container.Resolve<Services.IFiAccountService>();
            FiAccountsDetailPresentationModel pm = new FiAccountsDetailPresentationModel(dv, new BusinessEntities.FiAccount( e.Value.FiAccountID), service);
            dv.FlowDirection = (P2Translator.Culture.TextInfo.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight);
            dv.ShowDialog();
        }

        public void OnFiAccountFind(object sender, DataEventArgs<BusinessEntities.FiAccount> e)
        {
            if (listPresenter != null)
            {
                listPresenter.FindAccount(e.Value);
            }
        }

    }
}
